﻿using Microsoft.AspNetCore.Mvc;
using TeachlyQBank.DbServer;
using TeachlyQBank.Models;

namespace TeachlyQBank.Controllers
{
    public class PaperController : Controller
    {
        private readonly  QuestionServer _questionServer;
        private readonly QuestionTypeServer _questionTypeServer;
        private readonly PaperServer _paperServer;

        public PaperController(QuestionServer questionServer,QuestionTypeServer questionTypeServer,PaperServer paperServer)
        { 
             _questionServer = questionServer;
            _questionTypeServer = questionTypeServer;
            _paperServer = paperServer;
        }

        // 功能:指向手工组卷功能页面
        public IActionResult ManualTestPaper()
        {
            List<ViewModel_ManualTestPaper> data = new List<ViewModel_ManualTestPaper>();

            List<Question>questions = new List<Question>();
            questions = _questionServer.SltServer();
            List<QuestionType> questionTypes = new List<QuestionType>();
            questionTypes=_questionTypeServer.GetAllQuestionTypes();

            foreach (Question question in questions)
            {
                foreach (QuestionType questionType in questionTypes) 
                {
                    if (question.question_type == questionType.qt_id)
                    {
                        ViewModel_ManualTestPaper node = new ViewModel_ManualTestPaper();
                        node.question_id=question.question_id;
                        node.question_content=question.question_content;
                        node.question_difficulty=question.question_difficulty;
                        node.type_name=questionType.type_name;
                        node.type_score=questionType.type_score;    
                        data.Add(node);
                    }
                }
            }
            return View(data);
        }

        // 提交一个试卷的添加
        public IActionResult AddPaperSumbit(ViewModel_PaperSubmit data)
        {
            Guid paper_id = Guid.NewGuid();

            Paper paper = new Paper();
            paper.paper_id = paper_id.ToString();
            paper.paper_name = data.PaperName;
            _paperServer.AddPaper(paper);
            

            foreach (string questionid in data.QuestionIds)
            {
                Paper_Question question = new Paper_Question();
                question.paper_id=paper_id.ToString();
                question.relation_id = Guid.NewGuid().ToString();
                question.question_id = questionid;
                _paperServer.AddPaperQuestion(question);
            }

            return Redirect("ManualTestPaper");
        }

        // 返回为考试计划选择试卷的视图
        public IActionResult SelectPaper()
        {
            return View();
        }

        // 返回审阅试卷的视图
        public IActionResult JudgePaper(string paperId)
        {
            // 获取该试卷的信息
            List<Paper> paperList = new List<Paper>();
            paperList = _paperServer.GetAllPapers();
            Paper paper = new Paper();
            foreach (Paper node in paperList)
            {
                if (node.paper_id == paperId)
                {
                    paper = node; 
                    break;
                }
            }

            List<ViewModel_JudgePaper> data = new List<ViewModel_JudgePaper>();
            data = _paperServer.GetAllPQ(paperId);

            foreach (ViewModel_JudgePaper node in data)
            {
                node.questionTypes=_questionTypeServer.GetScoreById(node.question_type)[0];
                node.paper=paper;
            }
            return View(data);
        }

        // 返回展示所有试卷的视图
        public IActionResult ShowPaper()
        {
            List<Paper> paperList = new List<Paper>();
            paperList=_paperServer.GetAllPapers();
            return View(paperList);
        }
    }
}
